package cn.tedu.springmvc2.controller;

import cn.tedu.springmvc2.mapper.UserMapper;
import cn.tedu.springmvc2.pojo.dto.UserDTO;
import cn.tedu.springmvc2.pojo.entity.User;
import cn.tedu.springmvc2.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/users")
public class RESTfulUserController {
    @Autowired
    private UserMapper userMapper;
    //@RequestMapping(value = "/users",method = RequestMethod.POST)
    //@PostMapping("/users")//等价上面的
    @PostMapping
    public String insert(UserDTO userDTO){
        User user=new User();
        BeanUtils.copyProperties(userDTO,user);
        user.setCreated(new Date());
        int num=userMapper.insertUser(user);
        System.out.println("豪");
        return num>0?"注册成功":"注册失败";
    }
    //@RequestMapping(value = "/users",method = RequestMethod.PUT)
    //@PutMapping("users")
    @PutMapping
    public String update(UserDTO userDTO){
        User user=new User();
        BeanUtils.copyProperties(userDTO,user);
        int num=userMapper.updateUser(user);
        return num>0?"修改成功":"修改失败";
    }
    //@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
    //@DeleteMapping("/users/{id}")
    @PutMapping("/{id}")
    public String delete(@PathVariable Integer id){
        int num=userMapper.deleteUser(id);
        return num>0?"成功":"删除失败";
    }
    //@RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
    //@GetMapping("/users/{id}")
    @GetMapping("/{id}")
    public UserVO find(@PathVariable Integer id){
        return userMapper.findById(id);
    }
}
